package com.lr.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lr.entity.*;
import com.lr.mapper.IndexMapper;
import com.lr.service.IndexService;
import com.lr.util.JSONUtil;
import net.sf.jsqlparser.statement.create.table.Index;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class IndexServiceImpl implements IndexService {
    @Value("${PAGESIZE}")
    private Integer PAGESIZE;

    @Autowired
    private IndexMapper indexMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Menu> getHeadMenu() {
        List<Menu> list;
        if(!redisTemplate.hasKey("HeadMenu")){
            list=indexMapper.getHeadMenu();
            redisTemplate.opsForValue().set("HeadMenu",JSONUtil.ObjectToJson(list));
        }else {
            list=JSONUtil.jsonToObject((String) redisTemplate.opsForValue().get("HeadMenu"),ArrayList.class);
        }
        return list;
    }

    @Override
    public List<Menu> getRightMenu() {
        List<Menu> list;
        if(!redisTemplate.hasKey("RightMenu")){
            list=indexMapper.getRightMenu();
            for (Menu menu:list){
                menu.setArticleList(indexMapper.getArticleByType(menu.getType(),menu.getNum()));
            }
            redisTemplate.opsForValue().set("RightMenu",JSONUtil.ObjectToJson(list));
        }else {
            list=JSONUtil.jsonToObject((String) redisTemplate.opsForValue().get("RightMenu"),ArrayList.class);
        }
        return list;
    }

    @Override
    public List<Menu> getBottom1Menu() {
        List<Menu> list;
        if(!redisTemplate.hasKey("Bottom1Menu")){
            list=indexMapper.getBottom1Menu();
            for (Menu menu:list){
                menu.setArticleList(indexMapper.getArticleByType(menu.getType(),menu.getNum()));
            }
            redisTemplate.opsForValue().set("Bottom1Menu",JSONUtil.ObjectToJson(list));
        }else {
            list=JSONUtil.jsonToObject((String) redisTemplate.opsForValue().get("Bottom1Menu"),ArrayList.class);
        }
        return list;
    }

    @Override
    public List<Menu> getBottom2Menu() {
        List<Menu> list;
        if(!redisTemplate.hasKey("Bottom2Menu")){
            list=indexMapper.getBottom2Menu();
            for (Menu menu:list){
                menu.setArticleList(indexMapper.getArticleByType(menu.getType(),menu.getNum()));
            }
            redisTemplate.opsForValue().set("Bottom2Menu",JSONUtil.ObjectToJson(list));
        }else {
            list=JSONUtil.jsonToObject((String) redisTemplate.opsForValue().get("Bottom2Menu"),ArrayList.class);
        }
        return list;
    }

    @Override
    public Article getArticleById(Integer id) {
        return indexMapper.getArticleById(id);
    }

    @Override
    public List<Article> getArticlesByMenuType(Integer id) {
        return indexMapper.getArticleByType(id,PAGESIZE);
    }

    @Override
    public PageInfo getRecruit(Integer pageSize, Integer pageIndex) {
        PageHelper.startPage(pageIndex, pageSize);
        return new PageInfo(indexMapper.getRecruit());
    }

    @Override
    public PageInfo getResume(Integer pageSize, Integer pageIndex) {
        PageHelper.startPage(pageIndex, pageSize);
        return new PageInfo<>(indexMapper.getResume());
    }
    @Override
    public PageInfo getJobFair(Integer pageSize, Integer pageIndex) {
        PageHelper.startPage(pageIndex, pageSize);
        return new PageInfo<>(indexMapper.getJobFair());
    }
    @Override
    public PageInfo getNews(Integer pageSize, Integer pageIndex) {
        PageInfo pageInfo;
        if(!redisTemplate.hasKey("left1")){
            PageHelper.startPage(pageIndex, pageSize);
            pageInfo=new PageInfo(indexMapper.getNews());
            redisTemplate.opsForValue().set("left1",JSONUtil.ObjectToJson(pageInfo));
        }else {
            pageInfo=JSONUtil.jsonToObject((String) redisTemplate.opsForValue().get("left1"),PageInfo.class);
        }
        return pageInfo;
    }

    @Override
    public PageInfo<Recruit> getper(Integer pageSize, Integer pageIndex) {
        PageInfo pageInfo;
        if(!redisTemplate.hasKey("left2")){
            PageHelper.startPage(pageIndex, pageSize);
            pageInfo=new PageInfo(indexMapper.getPer());
            redisTemplate.opsForValue().set("left2",JSONUtil.ObjectToJson(pageInfo));
        }else {
            pageInfo=JSONUtil.jsonToObject((String) redisTemplate.opsForValue().get("left2"),PageInfo.class);
        }
        return pageInfo;
    }
    @Override
    public PageInfo getRecru(Integer pageSize, Integer pageIndex) {
        PageInfo pageInfo;
        if(!redisTemplate.hasKey("left3")){
            PageHelper.startPage(pageIndex, pageSize);
            pageInfo=new PageInfo(indexMapper.getRecruit());
            redisTemplate.opsForValue().set("left3",JSONUtil.ObjectToJson(pageInfo));
        }else {
            pageInfo=JSONUtil.jsonToObject((String) redisTemplate.opsForValue().get("left3"),PageInfo.class);
        }
        return pageInfo;
    }

    @Override
    public List<Resume> getResumeByLink(Resume resume) {
        return indexMapper.getResumeByLink(resume);
    }

    @Override
    public List<Recruit> getRecruitByLink(Recruit recruit) {
        return indexMapper.getRecruitByLink(recruit);
    }

    @Override
    public List<JobFair> getJobFairByLink(JobFair jobFair) {
        return indexMapper.getJobFairByLink(jobFair);
    }

    @Override
    public JobFair getJobFairById(Integer id) {
        return indexMapper.getJobFairById(id);
    }

    @Override
    public Recruit getRecruitById(Integer recruitId) {
        return indexMapper.getRecruitById(recruitId);
    }

    @Override
    public Resume getResumeById(Integer resumeId) {
        return indexMapper.getResumeById(resumeId);
    }
}
